package com.bl.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.bl.bean.BaiLianUserInfo;
import com.bl.bean.DeviceInfo;

public class BaiLianDao {
	
	public DeviceInfo getOneDevice(Connection c) {
		DeviceInfo deviceInfo = new DeviceInfo();
		String sql = "select mobile_name,mobile_type,sdk_int "
				+ " from android_version ,mobile_type \r\n" + 
				"order by rand()\r\n" + 
				"limit 1";
		Statement stmt = null;
		try {
			c.setAutoCommit(false);
			stmt = c.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				/*
				 * deviceBrand: samsung
					deviceModel: SM-G9350
					deviceVersion: 25
					version: 7.6.0
				 */
				deviceInfo.setDeviceBrand(rs.getString("mobile_name"));
				//deviceInfo.setDeviceid(rs.getString("deviceid"));
				deviceInfo.setDeviceModel(rs.getString("mobile_type"));
				deviceInfo.setDeviceVersion(rs.getString("sdk_int"));
			}
			stmt.close();
			c.commit();
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println(e.getClass().getName() + ": " + e.getMessage());
		}
		return deviceInfo;
	}
	
	public List<BaiLianUserInfo> getZhanghaoInfoNoLogin(Connection c){
		Statement stmt = null;
		List<BaiLianUserInfo> baiLianUserInfoList = new ArrayList<BaiLianUserInfo>();
		try {
			c.setAutoCommit(false);
			stmt = c.createStatement();
			String sql = "select code,pwd,member_token,member_id,member_name,loginstatus,deviceBrand,deviceModel,deviceVersion,deviceid "
					+ "from zhanghaoweb "
					+ "where loginstatus='0' "
					+ "or member_token is null or member_token = '' "
					+ "order by rand() "
					+ "limit 20 ";
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				BaiLianUserInfo baiLianUserInfo = new BaiLianUserInfo();
				baiLianUserInfo.setCode(rs.getString("code"));
				baiLianUserInfo.setPwd(rs.getString("pwd"));
				baiLianUserInfo.setMember_token(rs.getString("member_token"));
				baiLianUserInfo.setMember_id(rs.getString("member_id"));
				baiLianUserInfo.setMember_name(rs.getString("member_name"));
				baiLianUserInfo.setLoginstatus(rs.getString("loginstatus"));
				baiLianUserInfo.setDeviceBrand(rs.getString("deviceBrand"));
				baiLianUserInfo.setDeviceModel(rs.getString("deviceModel"));
				baiLianUserInfo.setDeviceVersion(rs.getString("deviceVersion"));
				baiLianUserInfo.setDeviceid(rs.getString("deviceid"));
				baiLianUserInfoList.add(baiLianUserInfo);
			}
			stmt.close();
			c.commit();
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println(e.getClass().getName() + ": " + e.getMessage());
		}
		return baiLianUserInfoList;
	}
	
	
	public int updateUserInfo(BaiLianUserInfo baiLianUserInfo,Connection c) {
		Statement stmt = null;
		int return_int = 0;
		try {
			c.setAutoCommit(false);
			stmt = c.createStatement();
			String sql = "update zhanghaoweb set "
					+ "member_token='"+baiLianUserInfo.getMember_token()+"',"
					+ "member_id='"+baiLianUserInfo.getMember_id()+"',"
					+ "member_name='"+baiLianUserInfo.getMember_name()+"',"
					+ "memberLevel='"+baiLianUserInfo.getMemberLevel()+"',"
					+ "loginstatus='1',"
					+ "logintime=now(),"
					+ "deviceBrand='"+baiLianUserInfo.getDeviceBrand()+"',"
					+ "deviceModel='"+baiLianUserInfo.getDeviceModel()+"',"
					+ "deviceVersion='"+baiLianUserInfo.getDeviceVersion()+"',"
					+ "deviceid='"+baiLianUserInfo.getDeviceid()+"',"
					+ "userinfo='"+baiLianUserInfo.getUserinfo()+"' "
					+ "where code='"+baiLianUserInfo.getCode()+"'";
			return_int = stmt.executeUpdate(sql);
			stmt.close();
			c.commit();
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println(e.getClass().getName() + ": " + e.getMessage());
		}
		return return_int;
	}
	
}
